package cap.dao.impl;

import cap.bean.SysCategory;
import cap.dao.SysCategoryDao;
import cap.db.DBPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SysCategoryDaoImpl implements SysCategoryDao {
	Connection conn=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	/**
	 *获取所有用户类别
	 */
	@Override
	public List<SysCategory> getAllSysCategory() {
		List<SysCategory> scList = new ArrayList<SysCategory>();
		SysCategory sc = null;
		try {
			String sql="select * from sys_category";
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while (rs.next()) {
				sc = new SysCategory();
				sc.setId(rs.getInt("id"));
				sc.setCategoryName(rs.getString("category_name"));
				sc.setArticles(rs.getInt("articals"));
				sc.setIsDelete(rs.getInt("is_delete"));
				scList.add(sc);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return scList;
	}
	/**
	 *根据id获取系统类别
	 */
	@Override
	public SysCategory getById(int id) {
		SysCategory sc = null;
		try {
			String sql="select * from sys_category where id=?";
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			ps.setInt(1,id);
			rs=ps.executeQuery();
			if (rs.next()) {
				sc = new SysCategory();
				sc.setId(rs.getInt("id"));
				sc.setCategoryName(rs.getString("category_name"));
				sc.setArticles(rs.getInt("articals"));
				sc.setIsDelete(rs.getInt("is_delete"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return sc;
	}
	/**
	 *根据id删除系统类别
	 */
	@Override
	public int deleteSysCategory(int scgId) {
		int res = -1;
		try {
			String sql="update sys_category set is_delete=1 where id=?";
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			ps.setInt(1,scgId);
			res=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return res;
	}
	/**
	 *根据系统类别名获取
	 */
	@Override
	public SysCategory getByName(String scgName) {
		SysCategory scg = null;
		try {
			String sql="select * from sys_category where category_name=? and is_delete=0";
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			ps.setString(1,scgName);
			rs=ps.executeQuery();
			if (rs.next()) {
				scg = new SysCategory();
				scg.setId(rs.getInt("id"));
				scg.setCategoryName(rs.getString("category_name"));
				scg.setArticles(rs.getInt("articals"));
				scg.setIsDelete(rs.getInt("is_delete"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return scg;
	}
	/**
	 *添加系统类别
	 */
	@Override
	public int insertSysCategory(String scgName) {
		int res = -1;
		try {
			String sql="insert into sys_category values(NULL, ?, 0, 0)";
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			ps.setString(1,scgName);
			res=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return res;
	}
	/**
	 *修改系统类别
	 */
	@Override
	public int updateSysCategory(int scgId, String scgName) {
		int res = -1;
		try {
			String sql="update sys_category set category_name=? where id=?";
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			ps.setString(1,scgName);
			ps.setInt(2,scgId);
			res=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs,ps,conn);
		}
		return res;
	}
	/**
	 *分页显示系统类别
	 */
	@Override
	public List<SysCategory> getSysCategoryByPage(int curPage, int size) {
		List<SysCategory> scgList = null;
		try {
			String sql = "select * from sys_category where is_delete=0 order by id DESC limit ?,?";
			scgList = new ArrayList<SysCategory>();
			conn = DBPool.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, curPage);
			ps.setInt(2, size);
			rs = ps.executeQuery();
			while (rs.next()) {
				SysCategory scg = new SysCategory();
				scg.setId(rs.getInt("id"));
				scg.setCategoryName(rs.getString("category_name"));
				scg.setArticles(rs.getInt("articals"));
				scg.setIsDelete(rs.getInt("is_delete"));
				scgList.add(scg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBPool.close(rs, ps, conn);
		}

		return scgList;
	}
	/**
	 *获取系统类别数
	 */
	@Override
	public int count(){
		int total=0;
		String sql="select count(*) from sys_category";
		try {
			conn=DBPool.getConn();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()) total=rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBPool.close(rs,ps,conn);
		}
		return total;
	}
}
